<?php

//后台的配置文件
include(dirname(__FILE__).'/config.php');



$dopost = isset($_REQUEST['dopost']) ? trim($_REQUEST['dopost']) : 'list';


/***************************************************************************/
if( 'add' == $dopost ){

	
	//默认值
	$art = array();
	$art['tid'] =  0 ;
	$art['typename'] =  '' ;
	$art['sort'] =  50 ;
	
	


	$action = '添加';
	include('temp/ad_type_upt.php');
/***************************************************************************/
}else if( 'upt' == $dopost ){
	
	$tid = isset($_REQUEST['tid']) ? intval($_REQUEST['tid']) : 0;
	//读取当前文章
	$sql = "select * from `ad_type` where tid='{$tid}'";
	$art = dbRow($sql);
	//var_dump($art);
	
	


	$action = '修改';
	include('temp/ad_type_upt.php');
/***************************************************************************/
}else if( 'save' == $dopost ){
	// addsave  uptsave 


	$tid = isset($_POST['tid']) ? intval($_POST['tid']) : 0;

	//var_dump($_POST);	
	$art = array();
	$art['typename'] =  trim($_POST['typename']) ;
	$art['sort'] =  intval($_POST['sort']) ;
	
	if(empty($art['typename'])){die('{"ret":999,"msg":"栏目名称不能为空！！！"}');} ;
	

	
	

	sec($art);// 递归的 sql 转义字符过滤 
	if($tid){
		//修改
		$action = '修改';
		$rs = dbUpdate('ad_type',$art," tid = '{$tid}'"); // 表 数据 条件

	}else{
		//添加
		$action = '添加';
		$rs = dbInsert('ad_type',$art); // 表 数据 
	}

//sleep(3);
	if($rs){
		//msgBox($action.'成功!!!' ,'?t='.time() , 2 );
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = $action.'成功!';
		$j['url'] = '?t='.time() ;

		die( json_encode($j) );

	}else{
		//msgBox($action.'失败!!!' ,'?t='.time() , 10 );
		
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '写入错误!';

		die( json_encode($j) );

	}



/***************************************************************************/
}else if( 'del' == $dopost ){
	$tid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;

	if($tid == 1){die('{"ret":999,"msg":"默认分类，禁止删除！！！"}');} ;
	
	if( dbList("select * from `ad` where tid = '{$tid}' limit 0,1 ") ){ die('{"ret":999,"msg":"分类下有广告，禁止删除！！！"}');}

	//真删除
	$rs = dbDelete('ad_type'," tid = '{$tid}'"); // 表 数据 条件
	


	if($rs){
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = '删除成功!!!';
		//$j['url'] = '?tid='.$art['tid'].'&t='.time() ;
		die( json_encode($j) );

	}else{
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '删除失败!!!';
		die( json_encode($j) );
	}
/***************************************************************************/
}else if( 'batch_sort' == $dopost ){
	
	$url = $_POST['url'];
	$sort_arr = $_POST['sort'];
	foreach($sort_arr as $k => $v){
		$art = array();
		$art['sort'] = $v;
		$rs = dbUpdate('ad_type',$art," tid = '{$k}'"); // 表 数据 条件
	}
	
	//msgBox('更新排序成功，刷新当前页面!!!' ,$url , 3 );
	$j = array();
	$j['ret'] = 0;
	$j['msg'] = '操作成功!!!';
	$j['url'] = $url ;
	die(json_encode($j));


	
	
	
/***************************************************************************/
}else if( 'list' == $dopost ){

    $typelist = dbList("select * from `ad_type` where 1 order by sort asc , tid asc ");


	include('temp/ad_type_list.php');
/***************************************************************************/
}else{
	die('404');
}
/***************************************************************************/
